import type { AxiosRequestConfig, AxiosResponse } from "axios";

export enum ErrorType {
  NETWORK_ERROR = "NETWORK_ERROR", // 网络错误
  HTTP_ERROR = "HTTP_ERROR", // HTTP错误 (4xx/5xx)
  BUSINESS_ERROR = "BUSINESS_ERROR", // 业务错误
  AUTH_ERROR = "AUTH_ERROR", // 认证错误
  PERMISSION_ERROR = "PERMISSION_ERROR", // 权限错误
  RESPONSE_PARSING_ERROR = "RESPONSE_PARSING_ERROR", //数据解析错误
}

// 错误信息接口
export interface ApiError {
  type: ErrorType;
  message: string;
  code?: number | string;
  response?: AxiosResponse;
  config?: AxiosRequestConfig;
}

// HTTP错误消息映射类型
export type HttpErrorMessages = Record<number, string>;

// 错误处理函数
export type ErrorHandler = (error: ApiError) => void;

/**
 * 默认HTTP错误消息映射
 *
 * 400: "请求参数错误"
 *
 * 401: "未授权，请登录"
 *
 * 403: "拒绝访问"
 *
 * ...
 */
export const DEFAULT_HTTP_ERROR_MESSAGES: HttpErrorMessages = {
  400: "请求参数错误",
  401: "未授权，请登录",
  403: "拒绝访问",
  404: "请求的资源不存在",
  405: "请求方法不允许",
  408: "请求超时",
  500: "服务器内部错误",
  501: "服务未实现",
  502: "网关错误",
  503: "服务不可用",
  504: "网关超时",
};
